package com.ebt.m.proposal_v2.dao.response;

import android.text.TextUtils;

import com.ebt.m.proposal_v2.base.StringExtractBase;
import com.ebt.m.proposal_v2.bean.InsuranceOption;
import com.ebt.m.proposal_v2.utils.EBTDataFormat;

import java.util.List;

/**
 * 附加险列表
 *
 * @author archer.qi
 *         Created on 2017/5/19.
 */
public class ResponseAttach extends StringExtractBase {

    /* 附加险ID*/
    public int productId;
    /*附加险名称*/
    public String productName;
    /* 缩略图*/
    public String thumbnail;
    /* 保额*/
    public String coverage;
    /* 保费*/
    public String premium;
    /*保障期间*/
    public String coveragePeriod;
    /*缴费期间*/
    public String paymentPeriod;
    /* 缴费期间-值*/
    public String paymentPeriodValue;
    /* 保障期间-值*/
    public String coveragePeriodValue;

    /***************** 新加字段，为豁免而生 ***************/

    /**
     * 险种代码： 主险的代码为*， 附加险的代码类似'MJ06'
     */
    public String planCode;

    /**
     * 附加险类型： 1 普通 2 豁免
     */
    public int riskType;

    /**
     * 豁免人：投保人Applicant  or 被保人Insured
     */
    public String exemptPerson;

    /**
     * 豁免险种：['*', 'JM6']
     */
    public List<String> exemptProduct;

    /***********************************************/

    public List<InsuranceOption> options;

    /*是否可用*/
    public int isEnable;
    /*不可用信息*/
    public String warnMsg;

    /* 是否被选中*/
    public boolean isChecked;

    public String accMaxAge;
    public String accMinAge;
    public String accOccupation;
    public String accSex;


    @Override
    public String getDisplayName() {
        return productName;
    }


    public void parseOptions() {
        if (options == null || options.size() == 0) {
            return;
        }
        for (InsuranceOption opt : options) {
            if (opt != null) {
                loadOption(opt);
            }
        }

    }

    private void loadOption(InsuranceOption opt) {
        if (opt.fieldName.equals("Coverage")) {
            coverage = opt.value;
            if (coverage == null || "".equals(coverage)) {
                coverage = opt.displayName;
            }
        }
        if (opt.fieldName.equals("Premium")) {
            // 保费存在小数点后多位的情况，这里对这种情况进行格式化处理，最多保留两位小数
            if (TextUtils.isEmpty(opt.value)) {
                premium = "0";
            } else {
                try {
                    premium = EBTDataFormat.formatNumber(Double.valueOf(opt.value));
                    opt.value = premium;
                } catch (Exception e) {
                    premium = "0";
                }
            }
            if (premium == null || "".equals(premium)) {
                premium = opt.displayName;
            }
        }
        if (opt.fieldName.equals("CoveragePeriod")) {
            coveragePeriod = opt.displayName;
            coveragePeriodValue = opt.value;
        }
        if (opt.fieldName.equals("PaymentPeriod")) {
            paymentPeriod = opt.displayName;
            paymentPeriodValue = opt.value;
        }
    }

    /**
     * 获取豁免人类型： 0 不豁免  1 豁免投保人 2 豁免被保人
     *
     * @return
     */
    public int getExemptionType() {
        if ("Applicant".equals(exemptPerson)) {
            return 1;
        }
        if ("Insured".equals(exemptPerson)) {
            return 2;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "{coveragePeriod: " + coveragePeriod + "\n"
                + "coveragePeriodValue: " + coveragePeriodValue + "\n"
                + "paymentPeriod: " + paymentPeriod + "\n"
                + "paymentPeriodValue: + " + paymentPeriodValue + "}";
    }
}
